package com.uduew.sjd.view;


import android.content.Context;
import android.content.res.ColorStateList;
import android.os.CountDownTimer;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

import com.uduew.sjd.R;
import com.uduew.sjd.utils.ResUtil;

public class CountDownTextView extends AppCompatTextView {

    private static final long DEFAULT_COUNTDOWN_INTERVAL = 1000; // 1秒间隔
    private static final long DEFAULT_COUNTDOWN_TIME = 60 * 1000; // 60秒总时间

    private CountDownTimer mCountDownTimer;
    private long mCountDownTime = DEFAULT_COUNTDOWN_TIME;
    private long mCountDownInterval = DEFAULT_COUNTDOWN_INTERVAL;
    private String mOriginalText;
    private ColorStateList mOriginalColor;

    private String mCountDownFormat = "(%ds)";
    private OnCountDownListener mOnCountDownListener;
    private boolean isCountingDown = false;


    public CountDownTextView(Context context) {
        super(context);
        init();
    }

    public CountDownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CountDownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void start() {
        if (!isCountingDown) {
            startCountDown();
        }
    }


    private void init() {
        mOriginalText = getText().toString();
        mOriginalColor = getTextColors();
        setOnClickListener(v -> {
            if (!isCountingDown) {
                if (mOnCountDownListener != null) {
                    mOnCountDownListener.onClick(CountDownTextView.this);
                }
            }
        });
    }

    /**
     * 开始倒计时
     */
    public void startCountDown() {
        if (isCountingDown) {
            return;
        }
        isCountingDown = true;
        setEnabled(false);
        setTextColor(ResUtil.INSTANCE.getColor(R.color.grey_50));
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        mCountDownTimer = new CountDownTimer(mCountDownTime, mCountDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                long seconds = millisUntilFinished / 1000;
                setText(String.format(mCountDownFormat, seconds));
                if (mOnCountDownListener != null) {
                    mOnCountDownListener.onTick(CountDownTextView.this, millisUntilFinished);
                }
            }

            @Override
            public void onFinish() {
                reset();
                if (mOnCountDownListener != null) {
                    mOnCountDownListener.onFinish(CountDownTextView.this);
                }
            }
        }.start();
    }

    /**
     * 重置倒计时
     */
    public void reset() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
        isCountingDown = false;
        setEnabled(true);
        setText(mOriginalText);
        setTextColor(mOriginalColor);
    }

    /**
     * 设置倒计时总时间
     *
     * @param millisInFuture 毫秒
     */
    public void setCountDownTime(long millisInFuture) {
        this.mCountDownTime = millisInFuture;
    }

    /**
     * 设置倒计时间隔
     *
     * @param countDownInterval 毫秒
     */
    public void setCountDownInterval(long countDownInterval) {
        this.mCountDownInterval = countDownInterval;
    }

    /**
     * 设置倒计时显示格式
     *
     * @param format 如 "%ds后重新获取"
     */
    public void setCountDownFormat(String format) {
        this.mCountDownFormat = format;
    }

    /**
     * 设置原始文本
     *
     * @param originalText 倒计时结束后显示的文本
     */
    public void setOriginalText(String originalText) {
        this.mOriginalText = originalText;
    }

    /**
     * 设置倒计时监听器
     *
     * @param listener 监听器
     */
    public void setOnCountDownListener(OnCountDownListener listener) {
        this.mOnCountDownListener = listener;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
    }

    /**
     * 倒计时监听接口
     */
    public interface OnCountDownListener {
        /**
         * 点击事件（倒计时开始时调用）
         *
         * @param view 当前View
         */
        void onClick(CountDownTextView view);

        /**
         * 倒计时进行中
         *
         * @param view                当前View
         * @param millisUntilFinished 剩余时间(毫秒)
         */
        void onTick(CountDownTextView view, long millisUntilFinished);

        /**
         * 倒计时结束
         *
         * @param view 当前View
         */
        void onFinish(CountDownTextView view);
    }
}